import React , { useReducer }from 'react';

// 更改状态的方法
const reduce = (prevState,action) => {
    let newState = {...prevState}
    if (action.type === 'minus') {
        newState.count--
    }
    if (action.type === 'add') {
        newState.count++
    }
    return newState
}
// 声明初始值
const intialState = {
    count:0
}

const App = () => {
    // 声明状态管理函数
    const [state, dispatch] = useReducer(reduce,intialState)
    return (
        <div>
            <h1>reduce</h1>
            <button onClick={() => {
                dispatch({
                    type:'minus'
                })
            }}> 
                -
            </button>
            {state.count}
            <button onClick={() => {
                dispatch({
                    type:'add'
                })
            }}>
                +
            </button>
        </div>
    );
}

export default App;
